package org.jeecg.modules.xm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhaodui.fxy.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.uwms.Uwmsdata;
import org.jeecg.modules.uwms.printUtil;
import org.jeecg.modules.uwms.printheader;
import org.jeecg.modules.uwms.printitem;
import org.jeecg.modules.xm.entity.*;
import org.jeecg.modules.xm.service.*;
import org.jeecg.modules.xm.vo.XmMain01Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.assertj.core.util.DateUtil.now;


/**
 * @Description: xm_main_01
 * @Author: zhaodui
 * @Date: 2020-03-14
 * @Version: V1.0
 */
@RestController
@RequestMapping("/xm/xmMain01")
@Slf4j
@Api(tags="贴息")
public class XmMain01Controller extends JeecgController<XmMain01, IXmMain01Service> {
    @Autowired
    private IXmBase01Service xmBase01Service;
    @Autowired
    private IXmMain01Service xmMain01Service;

    @Autowired
    private IXmItem02Service xmItem02Service;

    @Autowired
    private IXmItem01Service xmItem01Service;

    @Autowired
    private IXmBase03Service xmBase03Service;
    @Autowired
    private IXmBase05Service xmBase05Service;
    @Autowired
    private IXmBase02Service xmBase02Service;

    @Value("${jeecg.path.upload}")
    private String upLoadPath;
    /*---------------------------------主表处理-begin-------------------------------------*/

    /**
     * 分页列表查询
     *
     * @param xmMain01
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/list")
    public Result<?> queryPageList(XmMain01 xmMain01,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<XmMain01> queryWrapper = QueryGenerator.initQueryWrapper(xmMain01, req.getParameterMap());
        Page<XmMain01> page = new Page<XmMain01>(pageNo, pageSize);
        IPage<XmMain01> pageList = xmMain01Service.page(page, queryWrapper);
        List<XmMain01> listnew = new ArrayList<>();
        for(XmMain01 t:pageList.getRecords()){
            try{
                if(StringUtil.isEmpty(t.getQuery06())) {
                    QueryWrapper<XmBase01> queryWrapper01 = new QueryWrapper<XmBase01>();
                    queryWrapper01.eq("query03", t.getQuery03());
                    List<XmBase01> list01 = xmBase01Service.list(queryWrapper01);
                    t.setQuery06(list01.get(0).getQuery06());
                    t.setQuery07(list01.get(0).getQuery07());
                }
            }catch (Exception e){

            }
            try{

                QueryWrapper<XmBase02> queryWrapper02 = new QueryWrapper<XmBase02>();
                queryWrapper02.eq("query03", t.getQuery09());
                List<XmBase02> list02 = xmBase02Service.list(queryWrapper02);

                t.setQuery22(list02.get(0).getQuery22());
                t.setQuery23(list02.get(0).getQuery23());
                t.setQuery24(list02.get(0).getQuery24());
                t.setQuery25(list02.get(0).getQuery25());
                t.setQuery19(getmsg(t.getId()));

                t.setQuery26(list02.get(0).getQuery18());
                t.setQuery27(list02.get(0).getQuery19());
                t.setQuery28(list02.get(0).getQuery20());
                t.setQuery29(list02.get(0).getQuery21());
                t.setQuery30(list02.get(0).getQuery26());




            }catch (Exception e){

            }
            listnew.add(t);
        }
        pageList.setRecords(listnew);
        return Result.ok(pageList);
    }


    /**
     * 分页列表查询
     *
     * @param xmMain01
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/listytj")
    public Result<?> queryPageListytj(XmMain01 xmMain01,
                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                      HttpServletRequest req) {
        xmMain01.setQuery18("已提交");
        QueryWrapper<XmMain01> queryWrapper = QueryGenerator.initQueryWrapper(xmMain01, req.getParameterMap());
        Page<XmMain01> page = new Page<XmMain01>(pageNo, pageSize);
        IPage<XmMain01> pageList = xmMain01Service.page(page, queryWrapper);
        List<XmMain01> listnew = new ArrayList<>();
        for(XmMain01 t:pageList.getRecords()){

            try{
                if(StringUtil.isEmpty(t.getQuery06())) {
                    QueryWrapper<XmBase01> queryWrapper01 = new QueryWrapper<XmBase01>();
                    queryWrapper01.eq("query03", t.getQuery03());
                    List<XmBase01> list01 = xmBase01Service.list(queryWrapper01);
                    t.setQuery06(list01.get(0).getQuery06());
                    t.setQuery07(list01.get(0).getQuery07());
                }
            }catch (Exception e){

            }
            try{

                QueryWrapper<XmBase02> queryWrapper02 = new QueryWrapper<XmBase02>();
                queryWrapper02.eq("query03", t.getQuery09());
                List<XmBase02> list02 = xmBase02Service.list(queryWrapper02);

                t.setQuery22(list02.get(0).getQuery22());
                t.setQuery23(list02.get(0).getQuery23());
                t.setQuery24(list02.get(0).getQuery24());
                t.setQuery25(list02.get(0).getQuery25());
                t.setQuery19(getmsg(t.getId()));
                t.setQuery26(list02.get(0).getQuery18());
                t.setQuery27(list02.get(0).getQuery19());
                t.setQuery28(list02.get(0).getQuery20());
                t.setQuery29(list02.get(0).getQuery21());
                t.setQuery30(list02.get(0).getQuery26());

            }catch (Exception e){

            }
            listnew.add(t);
        }
        pageList.setRecords(listnew);
        return Result.ok(pageList);
    }


    @GetMapping(value = "/listyrl")
    @ApiOperation("登录接口")

     public Result<?> queryPageListyrl(XmMain01 xmMain01,
                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                      HttpServletRequest req) {
        xmMain01.setQuery18("已认领");
        String rluser = "";
        try{
            LoginUser sysUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
            System.out.println("sysUser+"+sysUser.getUsername()+sysUser.getRealname());

            rluser = sysUser.getUsername();
        }catch (Exception e){
        }
        if(StringUtil.isNotEmpty(rluser)){
            xmMain01.setQuery21(rluser);
        }
        QueryWrapper<XmMain01> queryWrapper = QueryGenerator.initQueryWrapper(xmMain01, req.getParameterMap());
        Page<XmMain01> page = new Page<XmMain01>(pageNo, pageSize);
        IPage<XmMain01> pageList = xmMain01Service.page(page, queryWrapper);

        List<XmMain01> listnew = new ArrayList<>();
        for(XmMain01 t:pageList.getRecords()){
            try{
                if(StringUtil.isEmpty(t.getQuery06())) {
                    QueryWrapper<XmBase01> queryWrapper01 = new QueryWrapper<XmBase01>();
                    queryWrapper01.eq("query03", t.getQuery03());
                    List<XmBase01> list01 = xmBase01Service.list(queryWrapper01);
                    t.setQuery06(list01.get(0).getQuery06());
                    t.setQuery07(list01.get(0).getQuery07());
                }
            }catch (Exception e){

            }
            try{

                QueryWrapper<XmBase02> queryWrapper02 = new QueryWrapper<XmBase02>();
                queryWrapper02.eq("query03", t.getQuery09());
                List<XmBase02> list02 = xmBase02Service.list(queryWrapper02);

                t.setQuery22(list02.get(0).getQuery22());
                t.setQuery23(list02.get(0).getQuery23());
                t.setQuery24(list02.get(0).getQuery24());
                t.setQuery25(list02.get(0).getQuery25());
                t.setQuery19(getmsg(t.getId()));
                t.setQuery26(list02.get(0).getQuery18());
                t.setQuery27(list02.get(0).getQuery19());
                t.setQuery28(list02.get(0).getQuery20());
                t.setQuery29(list02.get(0).getQuery21());
                t.setQuery30(list02.get(0).getQuery26());

            }catch (Exception e){

            }
            listnew.add(t);
        }
        pageList.setRecords(listnew);
        return Result.ok(pageList);
    }
    @GetMapping(value = "/listycs")
    public Result<?> queryPageListycs(XmMain01 xmMain01,
                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                      HttpServletRequest req) {
        xmMain01.setQuery18("已初审");
        QueryWrapper<XmMain01> queryWrapper = QueryGenerator.initQueryWrapper(xmMain01, req.getParameterMap());
        Page<XmMain01> page = new Page<XmMain01>(pageNo, pageSize);
        IPage<XmMain01> pageList = xmMain01Service.page(page, queryWrapper);
        List<XmMain01> listnew = new ArrayList<>();
        for(XmMain01 t:pageList.getRecords()){
            try{
                if(StringUtil.isEmpty(t.getQuery06())) {
                    QueryWrapper<XmBase01> queryWrapper01 = new QueryWrapper<XmBase01>();
                    queryWrapper01.eq("query03", t.getQuery03());
                    List<XmBase01> list01 = xmBase01Service.list(queryWrapper01);
                    t.setQuery06(list01.get(0).getQuery06());
                    t.setQuery07(list01.get(0).getQuery07());
                }
            }catch (Exception e){

            }
            try{

                QueryWrapper<XmBase02> queryWrapper02 = new QueryWrapper<XmBase02>();
                queryWrapper02.eq("query03", t.getQuery09());
                List<XmBase02> list02 = xmBase02Service.list(queryWrapper02);

                t.setQuery22(list02.get(0).getQuery22());
                t.setQuery23(list02.get(0).getQuery23());
                t.setQuery24(list02.get(0).getQuery24());
                t.setQuery25(list02.get(0).getQuery25());
                t.setQuery19(getmsg(t.getId()));
                t.setQuery26(list02.get(0).getQuery18());
                t.setQuery27(list02.get(0).getQuery19());
                t.setQuery28(list02.get(0).getQuery20());
                t.setQuery29(list02.get(0).getQuery21());
                t.setQuery30(list02.get(0).getQuery26());

            }catch (Exception e){

            }
            listnew.add(t);
        }
        pageList.setRecords(listnew);
        return Result.ok(pageList);
    }

    @GetMapping(value = "/listysj")
    public Result<?> queryPageListysj(XmMain01 xmMain01,
                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                      HttpServletRequest req) {
        xmMain01.setQuery18("已收件");
        QueryWrapper<XmMain01> queryWrapper = QueryGenerator.initQueryWrapper(xmMain01, req.getParameterMap());
        Page<XmMain01> page = new Page<XmMain01>(pageNo, pageSize);
        IPage<XmMain01> pageList = xmMain01Service.page(page, queryWrapper);
        List<XmMain01> listnew = new ArrayList<>();
        for(XmMain01 t:pageList.getRecords()){
            try{
                if(StringUtil.isEmpty(t.getQuery06())) {
                    QueryWrapper<XmBase01> queryWrapper01 = new QueryWrapper<XmBase01>();
                    queryWrapper01.eq("query03", t.getQuery03());
                    List<XmBase01> list01 = xmBase01Service.list(queryWrapper01);
                    t.setQuery06(list01.get(0).getQuery06());
                    t.setQuery07(list01.get(0).getQuery07());
                }
            }catch (Exception e){

            }
            try{

                QueryWrapper<XmBase02> queryWrapper02 = new QueryWrapper<XmBase02>();
                queryWrapper02.eq("query03", t.getQuery09());
                 List<XmBase02> list02 = xmBase02Service.list(queryWrapper02);

                t.setQuery22(list02.get(0).getQuery22());
                t.setQuery23(list02.get(0).getQuery23());
                t.setQuery24(list02.get(0).getQuery24());
                t.setQuery25(list02.get(0).getQuery25());
                t.setQuery19(getmsg(t.getId()));
                t.setQuery26(list02.get(0).getQuery18());
                t.setQuery27(list02.get(0).getQuery19());
                t.setQuery28(list02.get(0).getQuery20());
                t.setQuery29(list02.get(0).getQuery21());
                t.setQuery30(list02.get(0).getQuery26());

            }catch (Exception e){

            }

            listnew.add(t);
        }
        pageList.setRecords(listnew);
        return Result.ok(pageList);
    }

    public  String getmsg(String mainid){
String message ="1，在白名单。";
        XmMain01 xmMain01 = xmMain01Service.getById(mainid);
        String name = xmMain01.getQuery08();
        String uid = xmMain01.getQuery09();
        try {
             QueryWrapper<XmBase03> queryWrapper03 = new QueryWrapper<XmBase03>();
            queryWrapper03.eq("query14", name);
            queryWrapper03.eq("query13", uid);
            List<XmBase03> list03 = xmBase03Service.list(queryWrapper03);
            if (list03 == null || list03.size() == 0) {
                message = "1，企业不在白名单。";
            }
        } catch (Exception e) {
        }
//行业
        //人数
        //主营业务收入
        //营业收入
        String hangye = "";
        int renshu = 1;
        Double zhuyingyewu = 1.00;
        Double yingyeshouru = 1.00;
        try {
            hangye = xmMain01.getQuery12();
            renshu = Integer.parseInt(xmMain01.getQuery14());
            zhuyingyewu = Double.parseDouble(xmMain01.getQuery15());
            yingyeshouru = Double.parseDouble(xmMain01.getQuery13());
        } catch (Exception e) {

        }


        if (hangye.equals("交通运输业")) {
            if (yingyeshouru >= 30000||renshu >= 1000||(renshu >= 300 && yingyeshouru >= 3000)) {
                message = message + "2，请审核员审查小微企业划型。";
            }
        } else if (hangye.equals("住宿业")) {
            if (yingyeshouru >= 10000||renshu >= 300||(renshu >= 100 && yingyeshouru >= 2000)) {
                message = message + "2，请审核员审查小微企业划型。";
            }
        } else if (hangye.equals("餐饮业")) {
            if (yingyeshouru >= 10000||renshu >= 300||(renshu >= 100 && yingyeshouru >= 2000)) {
                message = message + "2，请审核员审查小微企业划型。";
            }
        } else if (hangye.equals("批发业")||hangye.equals("外贸业")) {
            if (yingyeshouru >= 40000||renshu >= 200||(renshu >= 20 && yingyeshouru >= 5000)) {
                message = message + "2，请审核员审查小微企业划型。";
            }
        } else if (hangye.equals("零售业")) {
            if (yingyeshouru >= 20000||renshu >= 300||(renshu >= 50 && yingyeshouru >= 500)) {
                message = message + "2，请审核员审查小微企业划型。";
            }
        } else {
            if (renshu >= 300) {
                message = message + "2，请审核员审查小微企业划型。";
            }
        }

        if ((zhuyingyewu / yingyeshouru) <= 0.7) {
            message = message + "3，主营业务收入额低于营业总收入70%。";

        }
return  message;
    }
    /**
     * 添加
     *
     * @param xmMain01
     * @return
     */
    @PostMapping(value = "/add")
    @AutoLog
    public Result<?> add(@RequestBody XmMain01 xmMain01) {
//        String message = "在白名单；";
//        try {
//            String name = xmMain01.getQuery08();
//            String uid = xmMain01.getQuery09();
//            QueryWrapper<XmBase03> queryWrapper03 = new QueryWrapper<XmBase03>();
//            queryWrapper03.eq("query14", name);
//            queryWrapper03.eq("query13", uid);
//            List<XmBase03> list03 = xmBase03Service.list(queryWrapper03);
//            if (list03 == null || list03.size() == 0) {
//                message = "企业不在白名单；";
//            }
//        } catch (Exception e) {
//        }
//
//        //行业
//        //人数
//        //主营业务收入
//        //营业收入
//        String hangye = "";
//        int renshu = 1;
//        int zhuyingyewu = 1;
//        int yingyeshouru = 1;
//        try {
//            hangye = xmMain01.getQuery12();
//            renshu = Integer.parseInt(xmMain01.getQuery14());
//            zhuyingyewu = Integer.parseInt(xmMain01.getQuery15());
//            yingyeshouru = Integer.parseInt(xmMain01.getQuery13());
//        } catch (Exception e) {
//
//        }
//
//
//        if (hangye.equals("交通运输业")) {
//            if (renshu > 300 && yingyeshouru > 3000) {
//                message = message + "很抱歉！签约人数不得多于300人，且营业总收入不得多于3000;";
//            }
//        } else if (hangye.equals("住宿业")) {
//            if (renshu > 100 && yingyeshouru > 2000) {
//                message = message + "很抱歉！签约人数不得多于100人，且营业总收入不得多于2000;";
//            }
//        } else if (hangye.equals("餐饮业")) {
//            if (renshu > 100 && yingyeshouru > 2000) {
//                message = message + "很抱歉！签约人数不得多于100人，且营业总收入不得多于2000;";
//            }
//        } else if (hangye.equals("批发业")) {
//            if (renshu > 20 && yingyeshouru > 5000) {
//                message = message + "很抱歉！签约人数不得多于20人，且营业总收入不得多于5000;";
//            }
//        } else if (hangye.equals("零售业")) {
//            if (renshu > 50 && yingyeshouru > 500) {
//                message = message + "很抱歉！签约人数不得多于50人，且营业总收入不得多于500;";
//            }
//        } else {
//            if (renshu > 100) {
//                message = message + "很抱歉！签约人数不得多于100人;";
//            }
//        }
//
//        if (zhuyingyewu / yingyeshouru <= 0.7) {
//            message = message + "主营业务收入额低于营业总收入70%;";
//
//        }


//        xmMain01.setQuery19(message);
        xmMain01Service.save(xmMain01);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param xmMain01
     * @return
     */
    @PutMapping(value = "/editbak")
    @AutoLog
    public Result<?> editbak(@RequestBody XmMain01 xmMain01) {
        xmMain01Service.updateById(xmMain01);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
//        xmMain01Service.delMain(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
//        this.xmMain01Service.delBatchMain(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }


    /**
     * 通过id和状态更改状态
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/getById")
    public Result<?> getById(@RequestParam(name = "id", required = true) String id, @RequestParam(name = "status", required = true) String status) {
        XmMain01 xmMain01 = xmMain01Service.getById(id);
        if (xmMain01 == null) {
            return Result.error("未找到对应数据");
        }

        if ("已认领".equals(xmMain01.getQuery19())) {
            return Result.error("其他人已认领");
        }
        xmMain01.setQuery19(status);
        xmMain01Service.updateById(xmMain01);//更改抬头状态
        QueryWrapper<XmItem02> queryWrapper02 = new QueryWrapper<XmItem02>();
        queryWrapper02.eq("query40", id);
        List<XmItem02> list02 = xmItem02Service.list(queryWrapper02);
        for (XmItem02 t : list02) {//更改项目信息
            t.setQuery14(status);
            xmItem02Service.updateById(t);
        }
        return Result.ok(xmMain01);
    }

    /**
     * 通过id和状态更改状态
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/changeById")
    @AutoLog
    public Result<?> changeById(@RequestParam(name = "id", required = true) String id,
                                @RequestParam(name = "status", required = true) String status,
                                            @RequestParam(name = "comment", required = false) String comment) {
        XmMain01 xmMain01 = xmMain01Service.getById(id);
        if (xmMain01 == null) {
            return Result.error("未找到对应数据");
        }
        xmMain01.setQuery18(status);

        XmItem01 xmItem01 = new XmItem01();
        xmItem01.setCreateTime(now());
        xmItem01.setQuery08(status);
        if(StringUtil.isNotEmpty(comment)){
            xmMain01.setQuery20(comment);
        }

        try{
            LoginUser sysUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
            xmMain01.setQuery21(sysUser.getUsername());
        }catch (Exception e){

        }


        xmItem01Service.save(xmItem01);
        xmMain01Service.updateById(xmMain01);//更改抬头状态
        QueryWrapper<XmItem02> queryWrapper02 = new QueryWrapper<XmItem02>();
        queryWrapper02.eq("query40", id);
        List<XmItem02> list02 = xmItem02Service.list(queryWrapper02);
        for (XmItem02 t : list02) {//更改项目信息
            t.setQuery13(status);
            xmItem02Service.updateById(t);
        }
        return Result.ok(xmMain01);
    }

//    /**
//     * 导出
//     *
//     * @return
//     */
//    @RequestMapping(value = "/exportXls")
//    public ModelAndView exportXls(HttpServletRequest request, XmMain01 xmMain01) {
//        return super.exportXls(request, xmMain01, XmMain01.class, "受疫情影响的小微企业贷款贴息明细表");
//    }

//    /**
//     * 导入
//     *
//     * @return
//     */
//    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
//    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
//        return super.importExcel(request, response, XmMain01.class);
//    }
    /*---------------------------------主表处理-end-------------------------------------*/


    /*--------------------------------子表处理-xm_item_02-begin----------------------------------------------*/

    /**
     * 查询子表信息 会传入主表ID
     *
     * @return
     */
    @GetMapping(value = "/listXmItem02ByMainId")
    public Result<?> listXmItem02ByMainId(XmItem02 xmItem02,
                                          @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                          HttpServletRequest req) {
        QueryWrapper<XmItem02> queryWrapper = QueryGenerator.initQueryWrapper(xmItem02, req.getParameterMap());
        Page<XmItem02> page = new Page<XmItem02>(pageNo, pageSize);
        IPage<XmItem02> pageList = xmItem02Service.page(page, queryWrapper);
        List<XmItem02> xmItem02Listnew = new ArrayList<>();

        for(XmItem02 t: pageList.getRecords()){
            String llbtget = "";
            //
            String message = "存在贷款记录";
            try {
                String htbh = t.getQuery14();
                QueryWrapper<XmBase05> queryWrapper05 = new QueryWrapper<XmBase05>();
                queryWrapper05.eq("query14", htbh);
//            queryWrapper05.eq("query14", uid);
                List<XmBase05> list05 = xmBase05Service.list(queryWrapper05);
                if (list05 == null || list05.size() == 0) {
                    message = "不存此贷款记录";
                } else {
                    llbtget = list05.get(0).getQuery25();
                }
            } catch (Exception e) {
            }
            t.setQuery19(message);
            t.setQuery20(llbtget);

            double llbt = 0.00;

            try {
                Double dklljd = 0.00;
                Double lpr = 0.00;//放款时点一年期LPR%
                Double dkje = 0.00;//贷款金额
                Double dkll = 0.00;//贷款利率
                int dkqx = 6;//期限
                lpr = Double.parseDouble(t.getQuery08());
                dkje = Double.parseDouble(t.getQuery01())*100;//贷款金额
                dkll = Double.parseDouble(t.getQuery07());
                dkqx = Integer.parseInt(t.getQuery04());//期限
                dklljd = dkll - lpr;//贷款利率基点= 贷款年利率%-放款时点一年期LPR%。
                if (dklljd >= 0 && dkqx >= 6) {   //贷款利率基点为正数的且期限不少于6个月的，利息补贴（元）为“贷款额*LPR数/4”；
                    llbt = dkje * lpr / 4;
                }
                if (dklljd >= 0 && dkqx < 6) {   //贷款利率基点为正数的且期限少于6个月的，利息补贴（元）为“贷款额*LPR数*贷款期限月数/24”；
                    llbt = dkje * lpr * dkqx / 24;
                }
                if (dklljd < 0 && dkqx >= 6) {   //贷款利率基点为负数的且期限不少于6个月的，利息补贴（元）为“贷款额*贷款利率/4”；
                    llbt = dkje * dkll / 4;
                }
                if (dklljd < 0 && dkqx < 6) {   //贷款利率基点为负数的且期限少于6个月的，利息补贴（元）为“贷款额*贷款利率*贷款期限月数/24”。
                    llbt = dkje * dkll * dkqx / 24;
                }
            }catch (Exception e){

            }
            t.setQuery21(Double.toString(llbt));



                xmItem02Listnew.add(t);
        }

        pageList.setRecords(xmItem02Listnew);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param xmItem02
     * @return
     */
    @PostMapping(value = "/addXmItem02")
    @AutoLog
    public Result<?> addXmItem02(@RequestBody XmItem02 xmItem02) {
        String llbtget = "";
        //
        String message = "存在贷款记录";
        double llbt = 0.00;

        try {
            Double dklljd = 0.00;
            Double lpr = 0.00;//放款时点一年期LPR%
            Double dkje = 0.00;//贷款金额
            Double dkll = 0.00;//贷款利率
            int dkqx = 6;//期限
            lpr = Double.parseDouble(xmItem02.getQuery08());
            dkje = Double.parseDouble(xmItem02.getQuery01())*100;
            dkll = Double.parseDouble(xmItem02.getQuery07());
            dkqx = Integer.parseInt(xmItem02.getQuery04());
            dklljd = dkll - lpr;//贷款利率基点= 贷款年利率%-放款时点一年期LPR%。
            if (dklljd >= 0 && dkqx >= 6) {   //贷款利率基点为正数的且期限不少于6个月的，利息补贴（元）为“贷款额*LPR数/4”；
                llbt = dkje * lpr / 4;
            }
            if (dklljd >= 0 && dkqx < 6) {   //贷款利率基点为正数的且期限少于6个月的，利息补贴（元）为“贷款额*LPR数*贷款期限月数/24”；
                llbt = dkje * lpr * dkqx / 24;
            }
            if (dklljd < 0 && dkqx >= 6) {   //贷款利率基点为负数的且期限不少于6个月的，利息补贴（元）为“贷款额*贷款利率/4”；
                llbt = dkje * dkll / 4;
            }
            if (dklljd < 0 && dkqx < 6) {   //贷款利率基点为负数的且期限少于6个月的，利息补贴（元）为“贷款额*贷款利率*贷款期限月数/24”。
                llbt = dkje * dkll * dkqx / 24;
            }


            try {
                String htbh = xmItem02.getQuery14();
                QueryWrapper<XmBase05> queryWrapper05 = new QueryWrapper<XmBase05>();
                queryWrapper05.eq("query14", htbh);
//            queryWrapper05.eq("query14", uid);
                List<XmBase05> list05 = xmBase05Service.list(queryWrapper05);
                if (list05 == null || list05.size() == 0) {
                    message = "不存此贷款记录";
                } else {
                    llbtget = list05.get(0).getQuery25();
                }
            } catch (Exception e) {
            }
        } catch (Exception e) {

        }
        xmItem02.setQuery19(message);
        xmItem02.setQuery20(llbtget);
        xmItem02.setQuery21(Double.toString(llbt));
        xmItem02Service.save(xmItem02);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param xmItem02
     * @return
     */
    @PutMapping(value = "/editXmItem02")
    @AutoLog
    public Result<?> editXmItem02(@RequestBody XmItem02 xmItem02) {
        xmItem02Service.updateById(xmItem02);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/deleteXmItem02")
    public Result<?> deleteXmItem02(@RequestParam(name = "id", required = true) String id) {
//        xmItem02Service.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/deleteBatchXmItem02")
    public Result<?> deleteBatchXmItem02(@RequestParam(name = "ids", required = true) String ids) {
//        this.xmItem02Service.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /*--------------------------------子表处理-xm_item_02-end----------------------------------------------*/

    /*--------------------------------子表处理-xm_item_01-begin----------------------------------------------*/

    /**
     * 查询子表信息 会传入主表ID
     *
     * @return
     */
    @GetMapping(value = "/listXmItem01ByMainId")
    public Result<?> listXmItem01ByMainId(XmItem01 xmItem01,
                                          @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                          HttpServletRequest req) {
        QueryWrapper<XmItem01> queryWrapper = QueryGenerator.initQueryWrapper(xmItem01, req.getParameterMap());
        Page<XmItem01> page = new Page<XmItem01>(pageNo, pageSize);
        IPage<XmItem01> pageList = xmItem01Service.page(page, queryWrapper);
        return Result.ok(pageList);
    }
    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/queryXmItem02ByMainId")
    public Result<?> queryXmItem02ListByMainId(@RequestParam(name="id",required=true) String id) {
        List<XmItem02> xmItem02List = xmItem02Service.selectByMainId(id);
        List<XmItem02> xmItem02Listnew = new ArrayList<>();
        for(XmItem02 t: xmItem02List){
            String llbtget = "";
            //
            String message = "存在贷款记录";
            try {
                String htbh = t.getQuery14();
                QueryWrapper<XmBase05> queryWrapper05 = new QueryWrapper<XmBase05>();
                queryWrapper05.eq("query14", htbh);
//            queryWrapper05.eq("query14", uid);
                List<XmBase05> list05 = xmBase05Service.list(queryWrapper05);
                if (list05 == null || list05.size() == 0) {
                    message = "不存此贷款记录";
                } else {
                    llbtget = list05.get(0).getQuery25();
                }
            } catch (Exception e) {
            }
            t.setQuery19(message);
            t.setQuery20(llbtget);

            double llbt = 0.00;
            try {
                Double dklljd = 0.00;
                Double lpr = 0.00;//放款时点一年期LPR%
                Double dkje = 0.00;//贷款金额
                Double dkll = 0.00;//贷款利率
                int dkqx = 6;//期限
                lpr = Double.parseDouble(t.getQuery08());
                dkje = Double.parseDouble(t.getQuery01())*100;//贷款金额
                dkll = Double.parseDouble(t.getQuery07());
                dkqx = Integer.parseInt(t.getQuery04());//期限
                dklljd = dkll - lpr;//贷款利率基点= 贷款年利率%-放款时点一年期LPR%。
                if (dklljd >= 0 && dkqx >= 6) {   //贷款利率基点为正数的且期限不少于6个月的，利息补贴（元）为“贷款额*LPR数/4”；
                    llbt = dkje * lpr / 4;
                }
                if (dklljd >= 0 && dkqx < 6) {   //贷款利率基点为正数的且期限少于6个月的，利息补贴（元）为“贷款额*LPR数*贷款期限月数/24”；
                    llbt = dkje * lpr * dkqx / 24;
                }
                if (dklljd < 0 && dkqx >= 6) {   //贷款利率基点为负数的且期限不少于6个月的，利息补贴（元）为“贷款额*贷款利率/4”；
                    llbt = dkje * dkll / 4;
                }
                if (dklljd < 0 && dkqx < 6) {   //贷款利率基点为负数的且期限少于6个月的，利息补贴（元）为“贷款额*贷款利率*贷款期限月数/24”。
                    llbt = dkje * dkll * dkqx / 24;
                }
            }catch (Exception e){

            }
            t.setQuery21(Double.toString(llbt));

            xmItem02Listnew.add(t);
        }


        return Result.ok(xmItem02Listnew);
    }
    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/queryXmItem01ByMainId")
    public Result<?> queryXmItem01ListByMainId(@RequestParam(name="id",required=true) String id) {
        List<XmItem01> xmItem01List = xmItem01Service.selectByMainId(id);
        return Result.ok(xmItem01List);
    }
    /**
     * 添加
     *
     * @param xmItem01
     * @return
     */
    @PostMapping(value = "/addXmItem01")
    @AutoLog
    public Result<?> addXmItem01(@RequestBody XmItem01 xmItem01) {
        xmItem01Service.save(xmItem01);
        return Result.ok("添加成功！");
    }


    /**
     *  编辑
     *
     * @param xmMain01Page
     * @return
     */
    @PutMapping(value = "/edit")
    @AutoLog
    public Result<?> editpage(@RequestBody XmMain01Page xmMain01Page) {
        XmMain01 xmMain01 = new XmMain01();
        BeanUtils.copyProperties(xmMain01Page, xmMain01);
        XmMain01 xmMain01Entity = xmMain01Service.getById(xmMain01.getId());
        if("已初审".equals(xmMain01.getQuery18())){
            for(XmItem02 t: xmMain01Page.getXmItem02List()){

                Double tx =  0.00;
                try{
                    tx = Double.parseDouble(t.getQuery22()) ;
                    if(tx==0.00){
                        return Result.error("手工输入贴息不能为0");
                    }
                }catch (Exception e){
                    return Result.error("手工输入贴息不合法");

                }

            }
        }



        if(xmMain01Entity==null) {
            return Result.error("未找到对应数据");
        }
        if("已认领".equals(xmMain01.getQuery18())){
            try{
                LoginUser sysUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();

                System.out.println("sysUser+"+sysUser.getUsername()+sysUser.getRealname());
                xmMain01.setQuery21(sysUser.getUsername());
            }catch (Exception e){

            }
        }

        xmMain01Service.updateMain(xmMain01, xmMain01Page.getXmItem02List(),xmMain01Page.getXmItem01List());
        return Result.ok("编辑成功!");
    }

    /**
     * 编辑
     *
     * @param xmItem01
     * @return
     */
    @PutMapping(value = "/editXmItem01")
    public Result<?> editXmItem01(@RequestBody XmItem01 xmItem01) {

        xmItem01Service.updateById(xmItem01);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/deleteXmItem01")
    public Result<?> deleteXmItem01(@RequestParam(name = "id", required = true) String id) {
//        xmItem01Service.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/deleteBatchXmItem01")
    @AutoLog
    public Result<?> deleteBatchXmItem01(@RequestParam(name = "ids", required = true) String ids) {
//        this.xmItem01Service.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /*--------------------------------子表处理-xm_item_01-end----------------------------------------------*/

    @RequestMapping(value = "/print/sksj/{id}", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    @ApiOperation(value = "根据ID生成收款收据", notes = "ID", httpMethod = "POST", produces = "application/json")
    public Result<?> batchdownhm(@PathVariable("id") String id) {
        Result<Uwmsdata> result = new Result<Uwmsdata>();
        OutputStream fileOut = null;
        BufferedImage bufferImg = null;
        String fileurl = "";
        ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
        String url = upLoadPath +"/excelmodel/sksj.xls";
        url = url.replace("D:/","D:/");

        HSSFWorkbook wb  = null;
        try {
            wb = new HSSFWorkbook(new FileInputStream(url));
        } catch (IOException e) {
            e.printStackTrace();
        }
        HSSFSheet sheet = wb.getSheetAt(0);
        sheet.setForceFormulaRecalculation(true);  //强制执行该sheet中所有公式
        sheet.protectSheet("Zd123168");

        int cellsNum = 0;
        int pagesize = 1;
        int page = 0;
        short high = 600;
        XmMain01 xmMain01 = xmMain01Service.getById(id);//企业主表
        if("已保存".equals(xmMain01.getQuery18())||"已提交".equals(xmMain01.getQuery18())||"已认领".equals(xmMain01.getQuery18())){
            return result.error500("单据状态为已保存,已提交,已认领时不允许生成");
        }
        String dwqc ="danwei ";
        String khyh ="khyh ";
        String zh ="zh ";
        String lxr ="lxr ";
        String sj ="sj ";
        dwqc = xmMain01.getQuery08();
        lxr = xmMain01.getQuery01();
        sj = xmMain01.getQuery03();
        try{
            QueryWrapper<XmBase02> queryWrapper1 = new QueryWrapper<XmBase02>();
            queryWrapper1.eq("query03", xmMain01.getQuery09());
            List<XmBase02> listbase01 =      xmBase02Service.list(queryWrapper1);

            khyh= listbase01.get(0).getQuery18()+listbase01.get(0).getQuery21();
            zh =  listbase01.get(0).getQuery19();
        }catch (Exception e){

        }
        List<XmItem02> xmItem02List = xmItem02Service.selectByMainId(id);//贴息申报

        Double txje = 0.00;
        for (XmItem02 t : xmItem02List) {
            txje = txje + Double.parseDouble( t.getQuery22());
        }

        Row rowColumnValue9 = sheet.getRow((short) 9); // 列名
        Cell cell1 = rowColumnValue9.getCell(5);
        cell1.setCellValue((Math.round(txje)));


        Row rowColumnValue10 = sheet.getRow((short) 10); // 列名
        Cell cell2 = rowColumnValue10.getCell(2);
        cell2.setCellValue(dwqc);


        Row rowColumnValue11 = sheet.getRow((short) 11); // 列名
        Cell cell3 = rowColumnValue11.getCell(2);
        cell3.setCellValue(khyh);
        Cell cell4 = rowColumnValue11.getCell(4);
        cell4.setCellValue(zh);

        Row rowColumnValue12 = sheet.getRow((short) 12); // 列名
        Cell cell5 = rowColumnValue12.getCell(2);
        cell5.setCellValue(lxr);

        Cell cell6 = rowColumnValue12.getCell(4);
        cell6.setCellValue(sj);

        try {
            // 第七步，将文件存到指定位置并打印
            String filest = DateUtils.date2Str(DateUtils.yyyymmddhhmmss);
            String savaPath = upLoadPath;
            fileurl = xmMain01.getQuery09()+"/"+ "sksj"  + "/" + filest + "/"+  "收款收据.xls";

            File file = new File(savaPath + "/"+xmMain01.getQuery09()+"/"+ "sksj"  + "/" + filest);
            savaPath = savaPath.replace("D:/","D:/");

            if (!file.exists()) {
                file.mkdirs();// 创建文件根目录
            }
            String outfilePath = savaPath + "/" + fileurl;
            try {
                FileOutputStream fileOutputStream = new FileOutputStream(outfilePath);//指定路径与名字和格式
                wb.write(fileOutputStream);//将数据写出去
                fileOutputStream.close();//关闭输出流
//                printUtil.printfile(outfilePath);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("----Excle文件已生成------");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileOut != null) {
                try {
                    fileOut.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result.success(fileurl) ;
    }


    /*--------------------------------子表处理-xm_item_01-end----------------------------------------------*/

    @RequestMapping(value = "/print/zjmx/{id}", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    @ApiOperation(value = "根据ID生成资金明细 ", notes = "ID", httpMethod = "POST", produces = "application/json")
    public Result<?> batchdownzjmx(@PathVariable("id") String id) {
        Result<Uwmsdata> result = new Result<Uwmsdata>();
        OutputStream fileOut = null;
        BufferedImage bufferImg = null;
        String fileurl = "";
        ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
        String url = upLoadPath +"/excelmodel/zjmx.xls";
        url = url.replace("D:/","D:/");

        HSSFWorkbook wb  = null;
        try {
            wb = new HSSFWorkbook(new FileInputStream(url));
        } catch (IOException e) {
            e.printStackTrace();
        }
        HSSFSheet sheet = wb.getSheetAt(0);
        sheet.setForceFormulaRecalculation(true);  //强制执行该sheet中所有公式
        sheet.protectSheet("Zd123168");
        int cellsNum = 2;
        int pagesize = 1;
        int page = 0;
        short high = 600;
        XmMain01 xmMain01 = xmMain01Service.getById(id);//企业主表
        if("已保存".equals(xmMain01.getQuery18())||"已提交".equals(xmMain01.getQuery18())||"已认领".equals(xmMain01.getQuery18())){
            return result.error500("单据状态为已保存,已提交,已认领时不允许生成");
        }
int xuhao = 1;
        List<XmItem02> xmItem02List = xmItem02Service.selectByMainId(id);//贴息申报
         for (XmItem02 t : xmItem02List) {
//            序号  0
//            所属街道 1
//            申请企业 2
//            1扶持政策 3
//            1扶持项目  4
//            扶持金额  5
//            1所属年度 6
//            企业住所所在区 7
//            所属行业 8
//            企业成立时间 9
//            统一社会信用代码 10
//            开户银行 11
//            银行账号 12
//            经办人 13
//            手机号码 14
//            电子邮箱 15
            String i0=  Integer.toString(xuhao);
            String i1= xmMain01.getQuery10();
            String i2= xmMain01.getQuery08();
            String i3= "厦府规〔2020〕5号市府促进企业复工复产八条措施的通知";
            String i4= "贷款贴息";
            String i5= t.getQuery22();
            String i6= "2020";
            String i7= "思明区";
            String i8= xmMain01.getQuery12();
            String i9= xmMain01.getQuery11();
            String i10= xmMain01.getQuery09();
             String i11 ="";
             String i12 ="";
//            03   找到18 19
             try{
                 QueryWrapper<XmBase02> queryWrapper1 = new QueryWrapper<XmBase02>();
                 queryWrapper1.eq("query03", i10);
                 List<XmBase02> listbase01 =      xmBase02Service.list(queryWrapper1);

                   i11= listbase01.get(0).getQuery18()+listbase01.get(0).getQuery21();
                   i12 =  listbase01.get(0).getQuery19();
             }catch (Exception e){

             }
            String i13= xmMain01.getQuery01();
            String i14= xmMain01.getQuery03();
            String i15= xmMain01.getQuery02();
            Row rowColumnValue = sheet.getRow((short) cellsNum); // 列名
              rowColumnValue.getCell(0).setCellValue(i0);
              rowColumnValue.getCell(1).setCellValue(i1);
              rowColumnValue.getCell(2).setCellValue(i2);
              rowColumnValue.getCell(3).setCellValue(i3);
              rowColumnValue.getCell(4).setCellValue(i4);
              rowColumnValue.getCell(5).setCellValue(i5);
              rowColumnValue.getCell(6).setCellValue(i6);
              rowColumnValue.getCell(7).setCellValue(i7);
              rowColumnValue.getCell(8).setCellValue(i8);
              rowColumnValue.getCell(9).setCellValue(i9);
              rowColumnValue.getCell(10).setCellValue(i10);
              rowColumnValue.getCell(11).setCellValue(i11);
              rowColumnValue.getCell(12).setCellValue(i12);
              rowColumnValue.getCell(13).setCellValue(i13);
              rowColumnValue.getCell(14).setCellValue(i14);
              rowColumnValue.getCell(15).setCellValue(i15);

             cellsNum++;
             xuhao++;

        }



        try {
            // 第七步，将文件存到指定位置并打印
            String filest = DateUtils.date2Str(DateUtils.yyyymmddhhmmss);
            String savaPath = upLoadPath;
            fileurl = xmMain01.getQuery09()+"/"+"zjmx" + "/" + filest + "/"+  "扶持资金明细表.xls";

            File file = new File(savaPath + "/" +xmMain01.getQuery09()+"/"+ "zjmx" + "/" + filest);
            savaPath = savaPath.replace("D:/","D:/");

            if (!file.exists()) {
                file.mkdirs();// 创建文件根目录
            }
            String outfilePath = savaPath + "/" + fileurl;
            try {
                FileOutputStream fileOutputStream = new FileOutputStream(outfilePath);//指定路径与名字和格式
                wb.write(fileOutputStream);//将数据写出去
                fileOutputStream.close();//关闭输出流
//                printUtil.printfile(outfilePath);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("----Excle文件已生成------");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileOut != null) {
                try {
                    fileOut.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result.success(fileurl) ;
    }

    @RequestMapping(value = "/print/txsq/{id}", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    @ApiOperation(value = "根据ID生成贴息申请表 ", notes = "ID", httpMethod = "POST", produces = "application/json")
    public Result<?> batchdowntxsq(@PathVariable("id") String id) {
        Result<Uwmsdata> result = new Result<Uwmsdata>();
        OutputStream fileOut = null;
        BufferedImage bufferImg = null;
        String fileurl = "";
        ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
        String url = upLoadPath +"/excelmodel/txsq.xls";
        url = url.replace("D:/","D:/");

        HSSFWorkbook wb  = null;
        try {
            wb = new HSSFWorkbook(new FileInputStream(url));
        } catch (IOException e) {
            e.printStackTrace();
        }
        HSSFSheet sheet = wb.getSheetAt(0);
        sheet.setForceFormulaRecalculation(true);  //强制执行该sheet中所有公式
        sheet.protectSheet("Zd123168");

        int cellsNum = 3;

        XmMain01 xmMain01 = xmMain01Service.getById(id);//企业主表
        if("已保存".equals(xmMain01.getQuery18())||"已提交".equals(xmMain01.getQuery18())||"已认领".equals(xmMain01.getQuery18())){
            return result.error500("单据状态为已保存,已提交,已认领时不允许生成");
        }



        //            序号  0
//            所属街道 1
//            申请企业 2
//            1扶持政策 3
//            1扶持项目  4
//            扶持金额  5
//            1所属年度 6
//            企业住所所在区 7
//            所属行业 8
//            企业成立时间 9
//            统一社会信用代码 10
//            开户银行 11
//            银行账号 12
//            经办人 13
//            手机号码 14
//            电子邮箱 15
         String i1= xmMain01.getQuery10();
        String i2= xmMain01.getQuery08();
        String i3= "厦府规〔2020〕5号市府促进企业复工复产八条措施的通知";
        String i4= "贷款贴息";
         String i6= "2020";
        String i7= "思明区";
        String i8= xmMain01.getQuery12();
        String i9= xmMain01.getQuery11();
        String i10= xmMain01.getQuery09();
        String i11 ="";
        String i12 ="";
//            03   找到18 19
        String i16= xmMain01.getQuery14();//申报月签约从业人数（人）
        String i17= xmMain01.getQuery13();//营业收入（万元）
        String i18= xmMain01.getQuery15();//主营业务收入（万元）
        try{
            QueryWrapper<XmBase02> queryWrapper1 = new QueryWrapper<XmBase02>();
            queryWrapper1.eq("query03", i10);
            List<XmBase02> listbase01 =      xmBase02Service.list(queryWrapper1);

            i11= listbase01.get(0).getQuery18()+listbase01.get(0).getQuery21();
            i12 =  listbase01.get(0).getQuery19();
        }catch (Exception e){

        }
        String i13= xmMain01.getQuery01();
        String i14= xmMain01.getQuery03();
        String i15= xmMain01.getQuery02();

        Row rowColumnValue1 = sheet.getRow((short) 1); // 列名
        rowColumnValue1.getCell(1).setCellValue(i2);
        rowColumnValue1.getCell(13).setCellValue(DateUtils.getDate("yyyy-MM-dd"));
        Row rowColumnValue2 = sheet.getRow((short) 2); // 列名
        rowColumnValue2.getCell(1).setCellValue(i10);
        rowColumnValue2.getCell(3).setCellValue(i8);
        rowColumnValue2.getCell(7).setCellValue(i11);
        rowColumnValue2.getCell(9).setCellValue(i12);
        rowColumnValue2.getCell(11).setCellValue(i16);
        rowColumnValue2.getCell(13).setCellValue(i17);
        rowColumnValue2.getCell(15).setCellValue(i18);

        int xuhao = 1;



        List<XmItem02> xmItem02List = xmItem02Service.selectByMainId(id);//贴息申报


        Double dkzh =0.00;
        Double xyflx =0.00;
        Double txje=0.00;
        CellStyle cs = wb.createCellStyle();
        cs.setWrapText(true);
        cs.setAlignment(CellStyle.ALIGN_CENTER);
        cs.setBorderLeft(CellStyle.BORDER_THIN);
        cs.setBorderRight(CellStyle.BORDER_THIN);
        cs.setBorderTop(CellStyle.BORDER_THIN);
        cs.setBorderBottom(CellStyle.BORDER_THIN);
        cs.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        for (XmItem02 t : xmItem02List) {
           if(cellsNum==3) {
               Row rowColumnValue = sheet.getRow((short) cellsNum); // 列名
               String it1 = "贷款发放额（万元）" + xuhao;
               String it2 = t.getQuery01();
               String it3 = "发放银行";
               String it4 = t.getQuery02();
               if("其他".equals(it4)){
                   it4 = t.getQuery15();
               }

               String it5 = "本年放款日期";
               String it6 = t.getQuery06();
               String it7 = "贷款使用期限（月）";
               String it8 = t.getQuery04();
               String it9 = "贷款年利率（%） ";
               String it10 = t.getQuery07();
               String it11 = "需应付利息（元）";
               String it12 = t.getQuery10();
               String it13 = "可享受贴息期限（月）";
               String it14 = "6";
               try{
                   int yuefen  = Integer.parseInt(t.getQuery03());
                   if(yuefen<6){
                       it14 = Integer.toString(yuefen) ;
                   }
               }catch (Exception e){

               }
try{
    dkzh = dkzh + Double.parseDouble(t.getQuery01());
    xyflx = xyflx + Double.parseDouble(t.getQuery10());
    txje= txje + Double.parseDouble(t.getQuery22());
}catch (Exception e){

}

               String it15 = "申请贴息金额（元）";
               String it16 = t.getQuery22();//手工维护


               rowColumnValue.getCell(0).setCellValue(it1);
               rowColumnValue.getCell(1).setCellValue(it2);
               rowColumnValue.getCell(2).setCellValue(it3);
               rowColumnValue.getCell(3).setCellValue(it4);
               rowColumnValue.getCell(4).setCellValue(it5);
               rowColumnValue.getCell(5).setCellValue(it6);
               rowColumnValue.getCell(6).setCellValue(it7);
               rowColumnValue.getCell(7).setCellValue(it8);
               rowColumnValue.getCell(8).setCellValue(it9);
               rowColumnValue.getCell(9).setCellValue(it10);
               rowColumnValue.getCell(10).setCellValue(it11);
               rowColumnValue.getCell(11).setCellValue(it12);
               rowColumnValue.getCell(12).setCellValue(it13);
               rowColumnValue.getCell(13).setCellValue(it14);
               rowColumnValue.getCell(14).setCellValue(it15);
               rowColumnValue.getCell(15).setCellValue(it16);
           }else{
               sheet.shiftRows( cellsNum, sheet.getLastRowNum(), 1, true, false);
               Row rowColumnValue = sheet.getRow((short) cellsNum); // 列名
               String it1 = "贷款发放额（万元）" + xuhao;
               String it2 = t.getQuery01();
               String it3 = "发放银行";
               String it4 = t.getQuery02();

               if("其他".equals(it4)){
                   it4 = t.getQuery15();
               }

               String it5 = "本年放款日期";
               String it6 = t.getQuery06();
               String it7 = "贷款使用期限（月）";
               String it8 = t.getQuery04();
               String it9 = "贷款年利率（%） ";
               String it10 = t.getQuery07();
               String it11 = "需应付利息（元）";
               String it12 = t.getQuery10();
               String it13 = "可享受贴息期限（月）";
               String it14 = "6";


               try{
                   int yuefen  = Integer.parseInt(t.getQuery03());
                   if(yuefen<6){
                       it14 = Integer.toString(yuefen) ;
                   }
               }catch (Exception e){

               }
               try{
                   dkzh = dkzh + Double.parseDouble(t.getQuery01());
                   xyflx = xyflx + Double.parseDouble(t.getQuery10());
                   txje= txje + Double.parseDouble(t.getQuery22());
               }catch (Exception e){

               }

               String it15 = "申请贴息金额（元）";
               String it16 = t.getQuery22();//手工维护
                rowColumnValue.setRowStyle(cs);

               Cell cell0 =  rowColumnValue.createCell(0);
               cell0.setCellValue(it1);
               Cell cell1 =  rowColumnValue.createCell(1);
               cell1 .setCellValue(it2);
               Cell cell2 =   rowColumnValue.createCell(2);
               cell2.setCellValue(it3);
               Cell cell3 =   rowColumnValue.createCell(3);
               cell3.setCellValue(it4);
               Cell cell4 =   rowColumnValue.createCell(4);
               cell4.setCellValue(it5);
               Cell cell5 =   rowColumnValue.createCell(5);
               cell5.setCellValue(it6);
               Cell cell6 =   rowColumnValue.createCell(6);
               cell6.setCellValue(it7);
               Cell cell7 =   rowColumnValue.createCell(7);
               cell7.setCellValue(it8);
               Cell cell8 =   rowColumnValue.createCell(8);
               cell8.setCellValue(it9);
               Cell cell9 =   rowColumnValue.createCell(9);
               cell9.setCellValue(it10);
               Cell cell10 =   rowColumnValue.createCell(10);
               cell10.setCellValue(it11);
               Cell cell11 =  rowColumnValue.createCell(11);
               cell11.setCellValue(it12);
               Cell cell12 =  rowColumnValue.createCell(12);
               cell12.setCellValue(it13);
               Cell cell13 =  rowColumnValue.createCell(13);
               cell13.setCellValue(it14);
               Cell cell14 =  rowColumnValue.createCell(14);
               cell14.setCellValue(it15);
               Cell cell15 =  rowColumnValue.createCell(15);
               cell15.setCellValue(it16);
               cell0.setCellStyle(cs);
               cell1.setCellStyle(cs);
               cell2.setCellStyle(cs);
               cell3.setCellStyle(cs);
               cell4.setCellStyle(cs);
               cell5.setCellStyle(cs);
               cell6.setCellStyle(cs);
               cell7.setCellStyle(cs);
               cell8.setCellStyle(cs);
               cell9.setCellStyle(cs);
               cell10.setCellStyle(cs);
               cell11.setCellStyle(cs);
               cell12.setCellStyle(cs);
               cell13.setCellStyle(cs);
               cell14.setCellStyle(cs);
               cell15.setCellStyle(cs);

           }
            cellsNum++;
            xuhao++;
        }

        Row rowColumnValuebotom = sheet.getRow((short) cellsNum); // 列名
        rowColumnValuebotom.getCell(1).setCellValue(dkzh);
        rowColumnValuebotom.getCell(11).setCellValue(xyflx);

        rowColumnValuebotom.getCell(15).setCellValue(txje);

        try {
            // 第七步，将文件存到指定位置并打印
            String filest = DateUtils.date2Str(DateUtils.yyyymmddhhmmss);
            String savaPath = upLoadPath;
            fileurl = xmMain01.getQuery09()+"/"+"txsq" + "/" + filest + "/"+ "贷款贴息申请表.xls";

            File file = new File(savaPath + "/" + xmMain01.getQuery09()+"/"+ "txsq"  + "/" + filest);
            savaPath = savaPath.replace("D:/","D:/");

            if (!file.exists()) {
                file.mkdirs();// 创建文件根目录
            }
            String outfilePath = savaPath + "/" + fileurl;
            try {
                FileOutputStream fileOutputStream = new FileOutputStream(outfilePath);//指定路径与名字和格式
                wb.write(fileOutputStream);//将数据写出去
                fileOutputStream.close();//关闭输出流
//                printUtil.printfile(outfilePath);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("----Excle文件已生成------");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileOut != null) {
                try {
                    fileOut.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result.success(fileurl) ;
    }

}
